package org.helsoft.mojo.api;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Bundle {

	private String groupId;
	private String artifactId;
	private final List<Version> versions = new ArrayList<Version>();
	private String fullName;

	public Bundle() {
	}

	public Bundle(String description) {
		fromString(description);
	}

	private void fromString(String description) {
		String[] elts = description.split("\\s*;\\s*");
		
		//Extraction of the groupId and artifactId
		if (elts.length > 0) {
			Pattern pattern = Pattern.compile("\\s*(.+)\\.([^\\.]+)\\s*");
			Matcher matcher = pattern.matcher(elts[0]);
			if (matcher.matches()) {
				groupId = matcher.group(1);
				artifactId = matcher.group(2);
				fullName = groupId + "." + artifactId;
			} else {
				groupId = elts[0].trim();
				artifactId = groupId;
				fullName = groupId;
			}
		}
		
		if (elts.length > 1) {
			for (int i = 1; i < elts.length ; ++i) {
				String config = elts[i].trim();
				if (config.startsWith("bundle-version")) {
					extractVersion(config);
				}
			}
		}		
	}

	private void extractVersion(String versionString) {
		Pattern pattern = Pattern.compile("bundle-version\\s*=\\s*\"\\[([^\\)]+)\\)\"");
		Matcher matcher = pattern.matcher(versionString);
		
		if (matcher.matches()) {
			for (String version : matcher.group(1).split("\\s*,\\s*")) {
				if (!version.trim().isEmpty()) {
					versions.add(new Version(version.trim()));
				}
			}
		} else {
			pattern = Pattern.compile("bundle-version\\s*=\\s*\"([^\"]+)\"");
			matcher = pattern.matcher(versionString);
			if (matcher.matches()) {
				versions.add(new Version(matcher.group(1)));
			}
		}
	}
	
	/**
	 * @return the groupId
	 */
	public final String getGroupId() {
		return groupId;
	}

	/**
	 * @param groupId
	 *            the groupId to set
	 */
	public final void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	/**
	 * @return the artifactId
	 */
	public final String getArtifactId() {
		return artifactId;
	}

	/**
	 * @param artifactId
	 *            the artifactId to set
	 */
	public final void setArtifactId(String artifactId) {
		this.artifactId = artifactId;
	}

	/**
	 * 
	 * @return The full name
	 */
	public final String getFullName() {
		return fullName;
	}

	public final void setFullName(String fullName) {
		this.fullName = fullName;
	}
	
	/**
	 * @return the versions
	 */
	public final List<Version> getVersions() {
		return Collections.unmodifiableList(versions);
	}
	
	public final void setVersion(String version) {
		setVersion(new Version(version));
	}

	public void setVersion(Version version) {
		versions.clear();
		versions.add(version);
	}

	@Override
	public String toString() {
		return String.format("%s:%s%s", groupId, artifactId,
				versions == null ? "" : (":" + versions));
	}

	public static Bundle fromFileName(String name) {
		Bundle result = null;
		String regex = "([^_]+)\\.([^\\._]+)_.*";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(name);
		if (matcher.matches()) {
			result = new Bundle();
			result.setGroupId(matcher.group(1));
			result.setArtifactId(matcher.group(2));
			result.setFullName(result.getGroupId() + "." + result.getArtifactId());
			result.setVersion(Version.fromFileName(name));
		}
		return result;
	}
	
}
